# -*- coding: utf-8 -*-
# @Time    : 2020/11/8 15:50
# @Author  : DuJiabao
# @File    : binary_search.py
# @Description : This is a program using binary search to find the index of a num in a sequence. 二分查找

def binary_search(n, nums):
    low, high = 0, len(nums) - 1
    while high >= low:
        mid = (high + low) // 2
        if nums[mid] > n:
            high = mid - 1
        elif nums[mid] < n:
            low = mid + 1
        else:
            return mid
    return False


if __name__ == '__main__':
    A = [1, 2, 4, 6, 8]
    print(binary_search(4, A))
